package cn.edu.gdgm.system.job;

import cn.edu.gdgm.system.enums.QosEnum;
import cn.edu.gdgm.system.utils.mqtt.client.EmqClient;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @author quan
 * @date 2021-02-27
 */
@Slf4j
public class PublishJob implements Job {
    @Resource
    private EmqClient emqClient;


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        String deviceUuid = jobDataMap.getString("deviceUuid");
        String lampPosition = jobDataMap.getString("lampPosition");
        String drugName = jobDataMap.getString("drugName");

        if (StringUtils.isNotBlank(deviceUuid) && StringUtils.isNotBlank(lampPosition)) {
            log.info("参数deviceUuid:{}, 参数lampPosition:{}", deviceUuid, lampPosition);

            String topic = deviceUuid + "/lamp";
            HashMap<String, String> map = new HashMap<>();
            map.put("msg", "ok");
            map.put("position", lampPosition);
            map.put("drugName", drugName);

            emqClient.connect();
            emqClient.publish(topic, JSON.toJSONString(map), QosEnum.Qos0, false);
            emqClient.disconnect();
        }

    }
}
